# Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
#
# Licensed under the BSD 3-Clause License (the "License"); you may not use this
# file except in compliance with the License. You may obtain a copy of the
# License at
#
# https://opensource.org/licenses/BSD-3-Clause
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

cc_library(
    name = "fiber",
    hdrs = [
        "alternatives.h",
        "async.h",
        "barrier.h",
        "condition_variable.h",
        "execution_context.h",
        "fiber.h",
        "fiber_local.h",
        "future.h",
        "latch.h",
        "logging.h",
        "mutex.h",
        "runtime.h",
        "semaphore.h",
        "shared_mutex.h",
        "this_fiber.h",
        "timer.h",
        "work_queue.h",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":alternatives",
        ":async",
        ":fiber_basic",
        ":future",
        ":logging",
        ":timer",
        ":work_queue",
    ],
)

cc_library(
    name = "async",
    hdrs = ["async.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":fiber_basic",
        "//flare/base:future",
    ],
)

cc_library(
    name = "timer",
    srcs = ["timer.cc"],
    hdrs = ["timer.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":fiber_basic",
        "//flare/base:function",
        "//flare/fiber/detail:fiber_impl",
    ],
)

cc_library(
    name = "future",
    hdrs = ["future.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":fiber_basic",
        "//flare/base:delayed_init",
        "//flare/base:future",
        "//flare/base/internal:time_view",
        "//flare/fiber/detail:fiber_impl",
    ],
)

cc_library(
    name = "logging",
    srcs = ["logging.cc"],
    hdrs = ["logging.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":fiber_basic",
        "//flare/base:string",
        "//flare/base/internal:logging",
    ],
)

cc_library(
    name = "alternatives",
    srcs = ["alternatives.cc"],
    hdrs = ["alternatives.h"],
    visibility = ["//visibility:public"],
)

#############################################
# TARGET BELOW ARE FOR INTERNAL USE.        #
#                                           #
# YOU SHOULD USE `fiber` ABOVE INSTEAD.     #
#############################################

cc_library(
    name = "fiber_basic",
    srcs = [
        "condition_variable.cc",
        "execution_context.cc",
        "fiber.cc",
        "latch.cc",
        "mutex.cc",
        "runtime.cc",
        "shared_mutex.cc",
        "this_fiber.cc",
    ],
    hdrs = [
        "barrier.h",
        "condition_variable.h",
        "execution_context.h",
        "fiber.h",
        "fiber_local.h",
        "latch.h",
        "mutex.h",
        "runtime.h",
        "semaphore.h",
        "shared_mutex.h",
        "this_fiber.h",
    ],
    deps = [
        "//flare/base:chrono",
        "//flare/base:deferred",
        "//flare/base:function",
        "//flare/base:likely",
        "//flare/base:logging",
        "//flare/base:random",
        "//flare/base:ref_ptr",
        "//flare/base:string",
        "//flare/base/internal:annotation",
        "//flare/base/internal:cpu",
        "//flare/base/internal:index_alloc",
        "//flare/base/object_pool:ref_counted",
        "//flare/base/thread:attribute",
        "//flare/base/thread:semaphore",
        "//flare/fiber/detail:fiber_impl",
        "//flare/fiber/detail:scheduling_parameters",
        "@com_github_gflags_gflags//:gflags",
    ],
)

cc_test(
    name = "fiber_test",
    srcs = ["fiber_test.cc"],
    deps = [
        ":fiber",
        "//flare/base:random",
        "//flare/base/internal:cpu",
        "@com_github_gflags_gflags//:gflags",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "condition_variable_test",
    srcs = ["condition_variable_test.cc"],
    deps = [
        ":fiber",
        "//flare/base:random",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "fiber_local_test",
    srcs = ["fiber_local_test.cc"],
    deps = [
        ":fiber",
        "//flare/base:random",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "fiber_local_benchmark",
    srcs = ["fiber_local_benchmark.cc"],
    deps = [
        ":fiber",
        "//flare:init",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_test(
    name = "latch_test",
    srcs = ["latch_test.cc"],
    deps = [
        ":fiber",
        "//flare/base:chrono",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "barrier_test",
    srcs = ["barrier_test.cc"],
    deps = [
        ":fiber",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "this_fiber_test",
    srcs = ["this_fiber_test.cc"],
    tags = ["exclusive"],
    deps = [
        ":fiber",
        "//flare/base:random",
        "//flare/fiber/detail:testing",
        "@com_github_gflags_gflags//:gflags",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "execution_context_test",
    srcs = ["execution_context_test.cc"],
    deps = [
        ":async",
        ":fiber",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "execution_context_benchmark",
    srcs = ["execution_context_benchmark.cc"],
    deps = [
        ":fiber",
        "//flare:init",
        "@com_github_google_benchmark//:benchmark_main",
    ],
)

cc_test(
    name = "async_test",
    srcs = ["async_test.cc"],
    deps = [
        ":fiber",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_library(
    name = "work_queue",
    srcs = ["work_queue.cc"],
    hdrs = ["work_queue.h"],
    visibility = [
        "//flare/rpc/internal:__pkg__",
    ],
    deps = [
        ":fiber_basic",
        "//flare/base:function",
    ],
)

cc_test(
    name = "work_queue_test",
    srcs = ["work_queue_test.cc"],
    deps = [
        ":fiber",
        ":work_queue",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "timer_test",
    srcs = ["timer_test.cc"],
    tags = ["exclusive"],
    deps = [
        ":fiber",
        ":timer",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "future_test",
    srcs = ["future_test.cc"],
    deps = [
        ":fiber",
        ":future",
        "//flare/base:random",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "logging_test",
    srcs = ["logging_test.cc"],
    deps = [
        ":fiber",
        "//flare/base:logging",
        "//flare/fiber:logging",
        "//flare/testing:main",
    ],
)

cc_test(
    name = "semaphore_test",
    srcs = ["semaphore_test.cc"],
    deps = [
        ":fiber_basic",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "shared_mutex_test",
    srcs = ["shared_mutex_test.cc"],
    deps = [
        ":fiber_basic",
        "//flare/base:random",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "runtime_test",
    srcs = ["runtime_test.cc"],
    deps = [
        ":fiber",
        "//flare/base/internal:cpu",
        "//flare/base/thread:attribute",
        "//flare/base/thread:latch",
        "@com_github_gflags_gflags//:gflags",
        "@com_google_googletest//:gtest_main",
    ],
)

cc_test(
    name = "alternatives_test",
    srcs = ["alternatives_test.cc"],
    deps = [
        ":fiber",
        "//flare/fiber/detail:testing",
        "@com_google_googletest//:gtest_main",
    ],
)
